package com.dxp.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.common.CommonUtils;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.MD5Utils;
import com.apache.tools.StrUtil;
import com.dxp.utils.StrUtils;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;


/**
 * @Classname SaveAuthPluginImpl
 * @Description TODO
 * @Date 2020/3/25 0:53
 * @Created by fpc
 */
public class SaveAuthPluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());

    @Override
    public Object beforeInvoke(Map<String, Object> paramMap) {
        String sysEname = StrUtil.doNull(String.valueOf(paramMap.get("dxSysIntRelation.sysEname")),"");
        String optTypes = StrUtil.doNull(String.valueOf(paramMap.get("optTypes")),"1");
        if("2".equals(optTypes)){
            //2=刷新重新获取appId
            String relationInfId = String.valueOf(paramMap.get("relationInfId"));
            Map<String, Object> result = new HashMap<String, Object>();
            if(StrUtil.isNull(relationInfId)){
                throw new BusinessException("relationInfId参数不对，请检查参数。");
            }
            Map<String,Object> whereMap = new HashMap<>();
            whereMap.put("dxSysIntRelation.w_infoId",relationInfId);
            ResultEntity resultEntity1 = IusPluginUtil.getInstance().select("obj", "s_dxSysIntRelation", whereMap);
            if(!Validator.isEmpty(resultEntity1.getEntity())){
                Map map = (Map) resultEntity1.getEntity();
                String applyCompany = String.valueOf(map.get("applyCompany"));
                String applySysEname = String.valueOf(map.get("applySysEname"));
                String fromInterfaceCode = String.valueOf(map.get("fromInterfaceCode"));
                Map<String,String> appMap =  createAppIdKeys(applyCompany,applySysEname,fromInterfaceCode);

                whereMap.put("dxSysIntRelation.toServerId",appMap.get("appId"));
                whereMap.put("dxSysIntRelation.toInterfaceCode",appMap.get("appKey"));
                ResultEntity resultEntity2 = IusPluginUtil.getInstance().saveInvoke("u_dxSysIntRelation", whereMap,false);
                log.info("appId刷新完成："+ JSONObject.fromObject(resultEntity2));

                result.put(CustomMethodPlugin.resultMark, "操作成功");
                result.put(CustomMethodPlugin.pluginInnerReturn, appMap);
            }else{
                throw new BusinessException("未找到授权对象。");
            }
            return result;
        }else{
            //1=首次授权、生成请求地址
            String requestUrl = "/data/";
            String requestCode = sysEname+ StrUtils.generateNonceStr(6);//系统编号+随机字符串
            requestUrl = requestUrl+requestCode+"/";
            String applyCompany = String.valueOf(paramMap.get("dxSysIntRelation.applyCompany"));
            String applySysEname = String.valueOf(paramMap.get("dxSysIntRelation.applySysEname"));
            String fromInterfaceCode = String.valueOf(paramMap.get("dxSysIntRelation.fromInterfaceCode"));
            Map<String,String> appMap =  createAppIdKeys(applyCompany,applySysEname,fromInterfaceCode);

            paramMap.put("dxSysIntRelation.toServerId",appMap.get("appId"));
            paramMap.put("dxSysIntRelation.toInterfaceCode",appMap.get("appKey"));

            paramMap.put("dxSysIntRelation.requestCode",requestCode);
            paramMap.put("dxSysIntRelation.requestUrl",requestUrl);
            return null;
        }
    }

    private Map<String,String> createAppIdKeys(String applyCompany,String applySysEname,String interFaceCode){
        log.info("获取AppId和AppKey[applyCompany="+applyCompany+"][applySysEname="+applySysEname+"][interFaceCode="+interFaceCode+"]");
        log.info("applyCompany："+applyCompany);
        log.info("applyCompany："+applyCompany);
        Map<String,String> resMap = new HashMap();
        String appId = MD5Utils.MD5((applyCompany+applySysEname));//MD5(客户端申请使用的单位标识+客户端申请使用的系统)
        String appKey = MD5Utils.MD5((appId+interFaceCode));//MD5(AppId+接口编号)
        resMap.put("appId",appId);
        resMap.put("appKey",appKey);
        log.info("获取AppId和AppKey信息："+resMap);
        return  resMap;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
